package com.simin.base.appnet.netother;

import android.net.ParseException;
import android.text.TextUtils;

import com.google.gson.JsonParseException;
import com.google.gson.stream.MalformedJsonException;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import retrofit2.adapter.rxjava.HttpException;


/**
 * date:2023/10/27
 * author:wsm
 * faucation:自定义异常信息
 */
public class CustomException {
    /**
     * 未知错误
     */
    public static final int UNKNOWN = 1000;

    /**
     * 解析错误
     */
    public static final int PARSE_ERROR = 1001;

    /**
     * 网络错误
     */
    public static final int NETWORK_ERROR = 1002;

    /**
     * 协议错误
     */
    public static final int HTTP_ERROR = 1003;

    /**
     * 处理系统异常，封装成ApiException
     * Throwable包含Error和Exception
     */
    public static ApiException handleException(Throwable e) {

        e.printStackTrace();//打印异常

        ApiException ex;
        if (e instanceof JsonParseException || e instanceof JSONException || e instanceof ParseException || e instanceof MalformedJsonException) {
            //解析错误
            ex = new ApiException(0, "解析异常");
            return ex;
        } else if (e instanceof SocketTimeoutException || e instanceof UnknownHostException) {
            //网络错误
            ex = new ApiException(0, "网络连接超时，请检查您的网络状态，稍后重试");
            return ex;
        } else if (e instanceof ConnectException) {
            //网络错误
            ex = new ApiException(0, "服务器连接失败");
            return ex;
        } else if (e instanceof HttpException) {
            //http返回错误
            HttpException exception = (HttpException) e;
            String message = exception.response().message();
            int code = exception.response().code();
            ex = new ApiException(code, !TextUtils.isEmpty(message) ? message : !TextUtils.isEmpty(getCodeStatus(code)) ? getCodeStatus(code) : TextUtils.isEmpty(message) ? "http " + code : message);
            return ex;
        } else {
            //未知错误
            ex = new ApiException(0, e.getMessage());
            return ex;
        }
    }

    public static String getCodeStatus(int code) {
        if (code == 400) {
            return "请求报文语法错误或者参数错误";
        }
        if (code == 401) {
            return "没有http认证信息或者认证失败";
        }
        if (code == 403) {
            return "服务器拒绝";
        }
        if (code == 404) {
            return "资源找不到";
        }
        if (code == 407) {
            return "请求者必须授权使用代理";
        }
        if (code == 500) {
            return "服务器故障";
        }
        if (code == 502) {
            return "网关错误";
        }
        if (code == 503) {
            return "服务器超负载，或者停机维护";
        }
        if (code == 504) {
            return "网关超时";
        }
        if (code == 505) {
            return "服务器不支持请求中所使用的 HTTP 协议版本";
        }
        if (code == 11001) {
            return "用户未登录，请登录后进行访问";
        }
        return "";
    }
}
